/*
 * main.cpp
 *
 *  Created on: 2013-1-19
 *      Author: heqiu
 */

#include "Global.h"

void drawPoint(int32_t iWinX, int32_t iWinY) {
	g_GlobalData.g_uiDrawNo++;
	if (g_GlobalData.g_uiDrawNo > 10000000) {
		g_GlobalData.g_uiDrawNo = 0;
	}
	GuudPoint pw(iWinX, iWinY);
	GuudPoint pm;
	g_GlobalData.g_FarmMap.transformToMap(pw, pm);
	uint32_t uiBuilding = g_GlobalData.g_FarmMap.getBuilding(pm.m_iX, pm.m_iY);
	if (uiBuilding > 0) {
		g_GlobalData.g_FarmScreen.drawMarkPoint(pw);
		g_GlobalData.g_FarmScreen.drawBuilding(g_GlobalData.g_FarmMap,
				g_GlobalData.g_uiDrawNo,
				g_GlobalData.g_BuildingManager.getBuilding(uiBuilding));
	}

}
class ShowThread: public GuudThread {
public:
	bool m_bExist;
	void run() {
		m_bExist = false;
		while (true) {
			if (m_bExist) {
				break;
			}
			g_GlobalData.g_FarmScreen.white();
			//显示坐标
			g_GlobalData.g_FarmScreen.m_lock.lock();
			g_GlobalData.g_FarmScreen.drawCoordinate(g_GlobalData.g_FarmMap,
					g_GlobalData.g_GuudFont);
			//循环可见坐标
			g_GlobalData.g_FarmScreen.cycle(g_GlobalData.g_FarmMap, drawPoint);
			//绘制控制面板
			g_GlobalData.g_FarmScreen.myDrawControl();

			g_GlobalData.g_FarmScreen.m_lock.unlock();
			g_GlobalData.g_FarmScreen.show();
			guud_sleep(100);
		}
	}
	void stop() {
		m_bExist = true;
	}
};
ShowThread g_ShowThread;
class MovingThread: public GuudThread {
public:
	bool m_bExist;
	void run() {
		m_bExist = false;
		while (true) {
			if (m_bExist) {
				break;
			}
			guud_sleep(100);
		}
	}
	void stop() {
		m_bExist = true;
	}
};
MovingThread g_MovingThread;

int main(int argc, char ** argv) {
	guud_init();
	g_GlobalData.g_GuudLog.setLogName("./farm.log.txt");
	g_GlobalData.g_GuudLog.log("[%s][%d]begin...", __FILE__, __LINE__);
	g_GlobalData.g_FarmConfig.loadFile("./conf/farm.xml");
	//加载字体
	g_GlobalData.g_GuudFont.init("lazy.ttf", 10);

	//初始化游戏窗口
	{
		bool bFlag = g_GlobalData.g_FarmScreen.init(
				g_GlobalData.g_FarmConfig.getWindowWidth(),
				g_GlobalData.g_FarmConfig.getWindowHeight(),
				g_GlobalData.g_FarmConfig.getWindowBPP(),
				g_GlobalData.g_FarmConfig.getWindowSize(),
				g_GlobalData.g_FarmConfig.getWindowCaption().c_str());
		g_GlobalData.g_FarmScreen.m_stPixelPos.m_iX =
				g_GlobalData.g_FarmConfig.getWindowX();
		g_GlobalData.g_FarmScreen.m_stPixelPos.m_iY =
				g_GlobalData.g_FarmConfig.getWindowY();
		if (!bFlag) {
			guud_exit(__FILE__, __LINE__, bFlag, "g_FarmScreen.init");
		}
	}
	//加载建筑种类
	g_GlobalData.g_FarmConfig.getBuildingType();
	//加载控制类
	g_GlobalData.g_FarmConfig.getFarmControl();

	//获取当前的地图编号
	{
		uint32_t uiMapNo = g_GlobalData.g_FarmConfig.getCurMap();
		FarmConfig stFarmMapConfig;
		char confname[64];
		sprintf(confname, "./conf/map_%u.xml", uiMapNo);
		stFarmMapConfig.loadFile(confname);
		bool bFlag = g_GlobalData.g_FarmMap.init(stFarmMapConfig.getMapShort(),
				stFarmMapConfig.getMapLong());
		if (!bFlag) {
			g_GlobalData.g_GuudLog.log("[%s][%d]err no=%u short=%u long=%u",
					__FILE__, __LINE__, uiMapNo, stFarmMapConfig.getMapShort(),
					stFarmMapConfig.getMapLong());
			guud_exit(__FILE__, __LINE__, bFlag, "g_MapUnit.init");
		}
		g_GlobalData.g_UnitAnimalPosition.init(stFarmMapConfig.getMapLong());
		g_GlobalData.g_UnitAnimalMoving.init(
				&g_GlobalData.g_UnitAnimalPosition);

		//加载建筑物
		stFarmMapConfig.getBuildings();
		//加载当前建设群需要的图片
		g_GlobalData.g_BuildingManager.loadImage();
		//添加建筑到地图
		g_GlobalData.g_BuildingManager.addToMap(g_GlobalData.g_FarmMap);
	}
	//g_FarmMap.printBuilding();

	//显示线程
	g_ShowThread.start();
	//移动线程
	g_MovingThread.start();

	g_GlobalData.g_GuudLog.log("[%s][%d]开始监听事件", __FILE__, __LINE__);
	g_GlobalData.g_FarmEvent.beginListen();
	g_ShowThread.stop();
	g_MovingThread.stop();
	printf("[%s][%d]\n", __FILE__, __LINE__);
	g_ShowThread.wait();
	g_MovingThread.wait();
	printf("[%s][%d]\n", __FILE__, __LINE__);
	g_GlobalData.g_GuudLog.log("[%s][%d]end.", __FILE__, __LINE__);
	guud_exit(__FILE__, __LINE__, 0, "");
	return 0;
}

